<?php
/**
 * SGoodsSpecModel.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/26
 * Time: 22:34
 */
namespace app\common\auto_curd\php_system\model;

use think\Db;

class SGoodsSpecModel
{
    /**
     * 自增id
     */
    protected $id = '';
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    
    /**
     * 是否上架（下架后用户不可见, 0否, 1是）
     */
    protected $is_shelves = '1';
    public function getIsShelves()
    {
        return $this->is_shelves;
    }
    public function setIsShelves($is_shelves)
    {
        $this->is_shelves = $is_shelves;
    }
    
    /**
     * 添加时间
     */
    protected $create_ts = '0';
    public function getCreateTs()
    {
        return $this->create_ts;
    }
    public function setCreateTs($create_ts)
    {
        $this->create_ts = $create_ts;
    }
    
    /**
     * 更新时间
     */
    protected $update_ts = '0';
    public function getUpdateTs()
    {
        return $this->update_ts;
    }
    public function setUpdateTs($update_ts)
    {
        $this->update_ts = $update_ts;
    }
    
    /**
     * 商品id
     */
    protected $goods_id = '0';
    public function getGoodsId()
    {
        return $this->goods_id;
    }
    public function setGoodsId($goods_id)
    {
        $this->goods_id = $goods_id;
    }
    
    /**
     * 库存
     */
    protected $inventory = '0';
    public function getInventory()
    {
        return $this->inventory;
    }
    public function setInventory($inventory)
    {
        $this->inventory = $inventory;
    }
    
    /**
     * 销售价格
     */
    protected $price = '0.00';
    public function getPrice()
    {
        return $this->price;
    }
    public function setPrice($price)
    {
        $this->price = $price;
    }
    
    /**
     * 重量（kg） 
     */
    protected $weight = '0.00';
    public function getWeight()
    {
        return $this->weight;
    }
    public function setWeight($weight)
    {
        $this->weight = $weight;
    }
    
    /**
     * 原价
     */
    protected $original_price = '0.00';
    public function getOriginalPrice()
    {
        return $this->original_price;
    }
    public function setOriginalPrice($original_price)
    {
        $this->original_price = $original_price;
    }
    
    /**
     * 编码
     */
    protected $coding = '';
    public function getCoding()
    {
        return $this->coding;
    }
    public function setCoding($coding)
    {
        $this->coding = $coding;
    }
    
    /**
     * 条形码
     */
    protected $barcode = '';
    public function getBarcode()
    {
        return $this->barcode;
    }
    public function setBarcode($barcode)
    {
        $this->barcode = $barcode;
    }
    
    /**
     * 规格图片
     */
    protected $images = '';
    public function getImages()
    {
        return $this->images;
    }
    public function setImages($images)
    {
        $this->images = $images;
    }
    
    /**
     * 规格值（json字符串存储）
     */
    protected $value = '';
    public function getValue()
    {
        return $this->value;
    }
    public function setValue($value)
    {
        $this->value = $value;
    }
    
    /**
     * constructor.
     * @param array $data
     */
    public function __construct($data=[])
    {

        // 自增id
        if(isset($data['id']))
        {
            $this->id = $data['id'];
        }

        // 是否上架（下架后用户不可见, 0否, 1是）
        if(isset($data['is_shelves']))
        {
            $this->is_shelves = $data['is_shelves'];
        }

        // 添加时间
        if(isset($data['create_ts']))
        {
            $this->create_ts = $data['create_ts'];
        }

        // 更新时间
        if(isset($data['update_ts']))
        {
            $this->update_ts = $data['update_ts'];
        }

        // 商品id
        if(isset($data['goods_id']))
        {
            $this->goods_id = $data['goods_id'];
        }

        // 库存
        if(isset($data['inventory']))
        {
            $this->inventory = $data['inventory'];
        }

        // 销售价格
        if(isset($data['price']))
        {
            $this->price = $data['price'];
        }

        // 重量（kg） 
        if(isset($data['weight']))
        {
            $this->weight = $data['weight'];
        }

        // 原价
        if(isset($data['original_price']))
        {
            $this->original_price = $data['original_price'];
        }

        // 编码
        if(isset($data['coding']))
        {
            $this->coding = $data['coding'];
        }

        // 条形码
        if(isset($data['barcode']))
        {
            $this->barcode = $data['barcode'];
        }

        // 规格图片
        if(isset($data['images']))
        {
            $this->images = $data['images'];
        }

        // 规格值（json字符串存储）
        if(isset($data['value']))
        {
            $this->value = $data['value'];
        }

    }
    
    /**
     * 获取分页信息
     * @param $where
     * @param array $extend
     * @return \think\Paginator
     */
    static public function paginate($where,$extend=[])
    {
        $handler = Db::table('s_goods_spec');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->paginate();
    }
    
    /**
     * 添加|修改
     * @param $flag
     * @param $data
     * @param array $extend
     * @return int|string
     */
    static public function save($flag,$data,$extend=[])
    {
        if(empty($flag))
        {
            // 添加
            $data['create_ts'] = time();
            return Db::table('s_goods_spec')
                ->insertGetId($data);
        }
        else
        {
            // 编辑
            $data['update_ts'] = time();
            return Db::table('s_goods_spec')
                ->where('id',$flag)
                ->update($data);
        }
    }
    
    /**
     * 获取详情
     * @param $where
     * @param array $extend
     * @return array|false|null|\PDOStatement|string|\think\Model
     */
    static public function get($where,$extend=[])
    {
        $handler = Db::table('s_goods_spec');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->find();
    }
    
    /**
     * 删除
     * @param $flag
     * @param array $extend
     * @return int
     */
    static public function delete($flag,$extend=[])
    {
        return Db::table('s_goods_spec')
            ->where('id',$flag)
            ->delete();
    }
}